home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / tclUnixAZ.c < prev    next >
C/C++ Source or Header  |  1993-07-08  |  50KB  |  1,971 lines

  1. /* 
  2.  * tclUnixAZ.c --
  3.  *
  4.  *    This file contains the top-level command procedures for
  5.  *    commands in the Tcl core that require UNIX facilities
  6.  *    such as files and process execution.  Much of the code
  7.  *    in this file is based on earlier versions contributed
  8.  *    by Karl Lehenbauer, Mark Diekhans and Peter da Silva.
  9.  *
  10.  * Copyright (c) 1991-1993 The Regents of the University of California.
  11.  * All rights reserved.
  12.  *
  13.  * Permission is hereby granted, without written agreement and without
  14.  * license or royalty fees, to use, copy, modify, and distribute this
  15.  * software and its documentation for any purpose, provided that the
  16.  * above copyright notice and the following two paragraphs appear in
  17.  * all copies of this software.
  18.  * 
  19.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  20.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  21.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  22.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23.  *
  24.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  25.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  26.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  27.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  28.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  29.  */
  30.  
  31. #ifndef lint
  32. static char rcsid[] = "$Header: /user6/ouster/tcl/RCS/tclUnixAZ.c,v 1.61 93/07/08 09:59:20 ouster Exp $ SPRITE (Berkeley)";
  33. #endif /* not lint */
  34.  
  35. #include "tclInt.h"
  36. #include "tclUnix.h"
  37.  
  38. /*
  39.  * The variable below caches the name of the current working directory
  40.  * in order to avoid repeated calls to getcwd.  The string is malloc-ed.
  41.  * NULL means the cache needs to be refreshed.
  42.  */
  43.  
  44. static char *currentDir =  NULL;
  45.  
  46. /*
  47.  * Prototypes for local procedures defined in this file:
  48.  */
  49.  
  50. static int        CleanupChildren _ANSI_ARGS_((Tcl_Interp *interp,
  51.                 int numPids, int *pidPtr, int errorId,
  52.                 int keepNewline));
  53. static char *        GetFileType _ANSI_ARGS_((int mode));
  54. static char *        GetOpenMode _ANSI_ARGS_((Tcl_Interp *interp,
  55.                 char *string, int *modePtr));
  56. static int        StoreStatData _ANSI_ARGS_((Tcl_Interp *interp,
  57.                 char *varName, struct stat *statPtr));
  58.  
  59. /*
  60.  *----------------------------------------------------------------------
  61.  *
  62.  * Tcl_CdCmd --
  63.  *
  64.  *    This procedure is invoked to process the "cd" Tcl command.
  65.  *    See the user documentation for details on what it does.
  66.  *
  67.  * Results:
  68.  *    A standard Tcl result.
  69.  *
  70.  * Side effects:
  71.  *    See the user documentation.
  72.  *
  73.  *----------------------------------------------------------------------
  74.  */
  75.  
  76.     /* ARGSUSED */
  77. int
  78. Tcl_CdCmd(dummy, interp, argc, argv)
  79.     ClientData dummy;            /* Not used. */
  80.     Tcl_Interp *interp;            /* Current interpreter. */
  81.     int argc;                /* Number of arguments. */
  82.     char **argv;            /* Argument strings. */
  83. {
  84.     char *dirName;
  85.     Tcl_DString buffer;
  86.     int result;
  87.  
  88.     if (argc > 2) {
  89.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  90.         " dirName\"", (char *) NULL);
  91.     return TCL_ERROR;
  92.     }
  93.  
  94.     if (argc == 2) {
  95.     dirName = argv[1];
  96.     } else {
  97.     dirName = "~";
  98.     }
  99.     dirName = Tcl_TildeSubst(interp, dirName, &buffer);
  100.     if (dirName == NULL) {
  101.     return TCL_ERROR;
  102.     }
  103.     if (currentDir != NULL) {
  104.     ckfree(currentDir);
  105.     currentDir = NULL;
  106.     }
  107.     result = TCL_OK;
  108.     if (chdir(dirName) != 0) {
  109.     Tcl_AppendResult(interp, "couldn't change working directory to \"",
  110.         dirName, "\": ", Tcl_PosixError(interp), (char *) NULL);
  111.     result = TCL_ERROR;
  112.     }
  113.     Tcl_DStringFree(&buffer);
  114.     return result;
  115. }
  116.  
  117. /*
  118.  *----------------------------------------------------------------------
  119.  *
  120.  * Tcl_CloseCmd --
  121.  *
  122.  *    This procedure is invoked to process the "close" Tcl command.
  123.  *    See the user documentation for details on what it does.
  124.  *
  125.  * Results:
  126.  *    A standard Tcl result.
  127.  *
  128.  * Side effects:
  129.  *    See the user documentation.
  130.  *
  131.  *----------------------------------------------------------------------
  132.  */
  133.  
  134.     /* ARGSUSED */
  135. int
  136. Tcl_CloseCmd(dummy, interp, argc, argv)
  137.     ClientData dummy;            /* Not used. */
  138.     Tcl_Interp *interp;            /* Current interpreter. */
  139.     int argc;                /* Number of arguments. */
  140.     char **argv;            /* Argument strings. */
  141. {
  142.     Interp *iPtr = (Interp *) interp;
  143.     OpenFile *oFilePtr;
  144.     int result = TCL_OK;
  145.     FILE *f;
  146.  
  147.     if (argc != 2) {
  148.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  149.         " fileId\"", (char *) NULL);
  150.     return TCL_ERROR;
  151.     }
  152.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  153.     return TCL_ERROR;
  154.     }
  155.     oFilePtr = iPtr->oFilePtrArray[fileno(f)];
  156.     iPtr->oFilePtrArray[fileno(f)] = NULL;
  157.  
  158.     /*
  159.      * First close the file (in the case of a process pipeline, there may
  160.      * be two files, one for the pipe at each end of the pipeline).
  161.      */
  162.  
  163.     if (oFilePtr->f2 != NULL) {
  164.     if (fclose(oFilePtr->f2) == EOF) {
  165.         Tcl_AppendResult(interp, "error closing \"", argv[1],
  166.             "\": ", Tcl_PosixError(interp), "\n", (char *) NULL);
  167.         result = TCL_ERROR;
  168.     }
  169.     }
  170.     if (fclose(oFilePtr->f) == EOF) {
  171.     Tcl_AppendResult(interp, "error closing \"", argv[1],
  172.         "\": ", Tcl_PosixError(interp), "\n", (char *) NULL);
  173.     result = TCL_ERROR;
  174.     }
  175.  
  176.     /*
  177.      * If the file was a connection to a pipeline, clean up everything
  178.      * associated with the child processes.
  179.      */
  180.  
  181.     if (oFilePtr->numPids > 0) {
  182.     if (CleanupChildren(interp, oFilePtr->numPids, oFilePtr->pidPtr,
  183.         oFilePtr->errorId, 0) != TCL_OK) {
  184.         result = TCL_ERROR;
  185.     }
  186.     }
  187.  
  188.     ckfree((char *) oFilePtr);
  189.     return result;
  190. }
  191.  
  192. /*
  193.  *----------------------------------------------------------------------
  194.  *
  195.  * Tcl_EofCmd --
  196.  *
  197.  *    This procedure is invoked to process the "eof" Tcl command.
  198.  *    See the user documentation for details on what it does.
  199.  *
  200.  * Results:
  201.  *    A standard Tcl result.
  202.  *
  203.  * Side effects:
  204.  *    See the user documentation.
  205.  *
  206.  *----------------------------------------------------------------------
  207.  */
  208.  
  209.     /* ARGSUSED */
  210. int
  211. Tcl_EofCmd(notUsed, interp, argc, argv)
  212.     ClientData notUsed;            /* Not used. */
  213.     Tcl_Interp *interp;            /* Current interpreter. */
  214.     int argc;                /* Number of arguments. */
  215.     char **argv;            /* Argument strings. */
  216. {
  217.     FILE *f;
  218.  
  219.     if (argc != 2) {
  220.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  221.         " fileId\"", (char *) NULL);
  222.     return TCL_ERROR;
  223.     }
  224.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  225.     return TCL_ERROR;
  226.     }
  227.     if (feof(f)) {
  228.     interp->result = "1";
  229.     } else {
  230.     interp->result = "0";
  231.     }
  232.     return TCL_OK;
  233. }
  234.  
  235. /*
  236.  *----------------------------------------------------------------------
  237.  *
  238.  * Tcl_ExecCmd --
  239.  *
  240.  *    This procedure is invoked to process the "exec" Tcl command.
  241.  *    See the user documentation for details on what it does.
  242.  *
  243.  * Results:
  244.  *    A standard Tcl result.
  245.  *
  246.  * Side effects:
  247.  *    See the user documentation.
  248.  *
  249.  *----------------------------------------------------------------------
  250.  */
  251.  
  252.     /* ARGSUSED */
  253. int
  254. Tcl_ExecCmd(dummy, interp, argc, argv)
  255.     ClientData dummy;            /* Not used. */
  256.     Tcl_Interp *interp;            /* Current interpreter. */
  257.     int argc;                /* Number of arguments. */
  258.     char **argv;            /* Argument strings. */
  259. {
  260.     int outputId;            /* File id for output pipe.  -1
  261.                      * means command overrode. */
  262.     int errorId;            /* File id for temporary file
  263.                      * containing error output. */
  264.     int *pidPtr;
  265.     int numPids, result, keepNewline;
  266.     int firstWord;
  267.  
  268.     /*
  269.      * Check for a leading "-keepnewline" argument.
  270.      */
  271.  
  272.     keepNewline = 0;
  273.     for (firstWord = 1; (firstWord < argc) && (argv[firstWord][0] == '-');
  274.         firstWord++) {
  275.     if (strcmp(argv[firstWord], "-keepnewline") == 0) {
  276.         keepNewline = 1;
  277.     } else if (strcmp(argv[firstWord], "--") == 0) {
  278.         firstWord++;
  279.         break;
  280.     } else {
  281.         Tcl_AppendResult(interp, "bad switch \"", argv[firstWord],
  282.             "\": must be -keepnewline or --", (char *) NULL);
  283.         return TCL_ERROR;
  284.     }
  285.     }
  286.  
  287.     if (argc <= firstWord) {
  288.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  289.         " ?switches? arg ?arg ...?\"", (char *) NULL);
  290.     return TCL_ERROR;
  291.     }
  292.  
  293.     /*
  294.      * See if the command is to be run in background;  if so, create
  295.      * the command, detach it, and return a list of pids.
  296.      */
  297.  
  298.     if ((argv[argc-1][0] == '&') && (argv[argc-1][1] == 0)) {
  299.     int i;
  300.     char id[50];
  301.  
  302.     argc--;
  303.     argv[argc] = NULL;
  304.     numPids = Tcl_CreatePipeline(interp, argc-firstWord, argv+firstWord,
  305.         &pidPtr, (int *) NULL, (int *) NULL, (int *) NULL);
  306.     if (numPids < 0) {
  307.         return TCL_ERROR;
  308.     }
  309.     Tcl_DetachPids(numPids, pidPtr);
  310.     for (i = 0; i < numPids; i++) {
  311.         sprintf(id, "%d", pidPtr[i]);
  312.         Tcl_AppendElement(interp, id);
  313.     }
  314.     ckfree((char *) pidPtr);
  315.     return TCL_OK;
  316.     }
  317.  
  318.     /*
  319.      * Create the command's pipeline.
  320.      */
  321.  
  322.     numPids = Tcl_CreatePipeline(interp, argc-firstWord, argv+firstWord,
  323.         &pidPtr, (int *) NULL, &outputId, &errorId);
  324.     if (numPids < 0) {
  325.     return TCL_ERROR;
  326.     }
  327.  
  328.     /*
  329.      * Read the child's output (if any) and put it into the result.
  330.      */
  331.  
  332.     result = TCL_OK;
  333.     if (outputId != -1) {
  334.     while (1) {
  335. #        define BUFFER_SIZE 1000
  336.         char buffer[BUFFER_SIZE+1];
  337.         int count;
  338.     
  339.         count = read(outputId, buffer, (size_t) BUFFER_SIZE);
  340.     
  341.         if (count == 0) {
  342.         break;
  343.         }
  344.         if (count < 0) {
  345.         Tcl_ResetResult(interp);
  346.         Tcl_AppendResult(interp,
  347.             "error reading from output pipe: ",
  348.             Tcl_PosixError(interp), (char *) NULL);
  349.         result = TCL_ERROR;
  350.         break;
  351.         }
  352.         buffer[count] = 0;
  353.         Tcl_AppendResult(interp, buffer, (char *) NULL);
  354.     }
  355.     close(outputId);
  356.     }
  357.  
  358.     if (CleanupChildren(interp, numPids, pidPtr, errorId, keepNewline)
  359.         != TCL_OK) {
  360.     result = TCL_ERROR;
  361.     }
  362.     return result;
  363. }
  364.  
  365. /*
  366.  *----------------------------------------------------------------------
  367.  *
  368.  * Tcl_ExitCmd --
  369.  *
  370.  *    This procedure is invoked to process the "exit" Tcl command.
  371.  *    See the user documentation for details on what it does.
  372.  *
  373.  * Results:
  374.  *    A standard Tcl result.
  375.  *
  376.  * Side effects:
  377.  *    See the user documentation.
  378.  *
  379.  *----------------------------------------------------------------------
  380.  */
  381.  
  382.     /* ARGSUSED */
  383. int
  384. Tcl_ExitCmd(dummy, interp, argc, argv)
  385.     ClientData dummy;            /* Not used. */
  386.     Tcl_Interp *interp;            /* Current interpreter. */
  387.     int argc;                /* Number of arguments. */
  388.     char **argv;            /* Argument strings. */
  389. {
  390.     int value;
  391.  
  392.     if ((argc != 1) && (argc != 2)) {
  393.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  394.         " ?returnCode?\"", (char *) NULL);
  395.     return TCL_ERROR;
  396.     }
  397.     if (argc == 1) {
  398.     exit(0);
  399.     }
  400.     if (Tcl_GetInt(interp, argv[1], &value) != TCL_OK) {
  401.     return TCL_ERROR;
  402.     }
  403.     exit(value);
  404.     /*NOTREACHED*/
  405.     return TCL_OK;            /* Better not ever reach this! */
  406. }
  407.  
  408. /*
  409.  *----------------------------------------------------------------------
  410.  *
  411.  * Tcl_FileCmd --
  412.  *
  413.  *    This procedure is invoked to process the "file" Tcl command.
  414.  *    See the user documentation for details on what it does.
  415.  *
  416.  * Results:
  417.  *    A standard Tcl result.
  418.  *
  419.  * Side effects:
  420.  *    See the user documentation.
  421.  *
  422.  *----------------------------------------------------------------------
  423.  */
  424.  
  425.     /* ARGSUSED */
  426. int
  427. Tcl_FileCmd(dummy, interp, argc, argv)
  428.     ClientData dummy;            /* Not used. */
  429.     Tcl_Interp *interp;            /* Current interpreter. */
  430.     int argc;                /* Number of arguments. */
  431.     char **argv;            /* Argument strings. */
  432. {
  433.     char *p;
  434.     int length, statOp, result;
  435.     int mode = 0;            /* Initialized only to prevent
  436.                      * compiler warning message. */
  437.     struct stat statBuf;
  438.     char *fileName, c;
  439.     Tcl_DString buffer;
  440.  
  441.     if (argc < 3) {
  442.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  443.         " option name ?arg ...?\"", (char *) NULL);
  444.     return TCL_ERROR;
  445.     }
  446.     c = argv[1][0];
  447.     length = strlen(argv[1]);
  448.     result = TCL_OK;
  449.  
  450.     /*
  451.      * First handle operations on the file name.
  452.      */
  453.  
  454.     fileName = Tcl_TildeSubst(interp, argv[2], &buffer);
  455.     if (fileName == NULL) {
  456.     result = TCL_ERROR;
  457.     goto done;
  458.     }
  459.     if ((c == 'd') && (strncmp(argv[1], "dirname", length) == 0)) {
  460.     if (argc != 3) {
  461.         argv[1] = "dirname";
  462.         not3Args:
  463.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  464.             " ", argv[1], " name\"", (char *) NULL);
  465.         result = TCL_ERROR;
  466.         goto done;
  467.     }
  468.     p = strrchr(fileName, '/');
  469.     if (p == NULL) {
  470.         interp->result = ".";
  471.     } else if (p == fileName) {
  472.         interp->result = "/";
  473.     } else {
  474.         *p = 0;
  475.         Tcl_SetResult(interp, fileName, TCL_VOLATILE);
  476.         *p = '/';
  477.     }
  478.     goto done;
  479.     } else if ((c == 'r') && (strncmp(argv[1], "rootname", length) == 0)
  480.         && (length >= 2)) {
  481.     char *lastSlash;
  482.  
  483.     if (argc != 3) {
  484.         argv[1] = "rootname";
  485.         goto not3Args;
  486.     }
  487.     p = strrchr(fileName, '.');
  488.     lastSlash = strrchr(fileName, '/');
  489.     if ((p == NULL) || ((lastSlash != NULL) && (lastSlash > p))) {
  490.         Tcl_SetResult(interp, fileName, TCL_VOLATILE);
  491.     } else {
  492.         *p = 0;
  493.         Tcl_SetResult(interp, fileName, TCL_VOLATILE);
  494.         *p = '.';
  495.     }
  496.     goto done;
  497.     } else if ((c == 'e') && (strncmp(argv[1], "extension", length) == 0)
  498.         && (length >= 3)) {
  499.     char *lastSlash;
  500.  
  501.     if (argc != 3) {
  502.         argv[1] = "extension";
  503.         goto not3Args;
  504.     }
  505.     p = strrchr(fileName, '.');
  506.     lastSlash = strrchr(fileName, '/');
  507.     if ((p != NULL) && ((lastSlash == NULL) || (lastSlash < p))) {
  508.         Tcl_SetResult(interp, p, TCL_VOLATILE);
  509.     }
  510.     goto done;
  511.     } else if ((c == 't') && (strncmp(argv[1], "tail", length) == 0)
  512.         && (length >= 2)) {
  513.     if (argc != 3) {
  514.         argv[1] = "tail";
  515.         goto not3Args;
  516.     }
  517.     p = strrchr(fileName, '/');
  518.     if (p != NULL) {
  519.         Tcl_SetResult(interp, p+1, TCL_VOLATILE);
  520.     } else {
  521.         Tcl_SetResult(interp, fileName, TCL_VOLATILE);
  522.     }
  523.     goto done;
  524.     }
  525.  
  526.     /*
  527.      * Next, handle operations that can be satisfied with the "access"
  528.      * kernel call.
  529.      */
  530.  
  531.     if (fileName == NULL) {
  532.     result = TCL_ERROR;
  533.     goto done;
  534.     }
  535.     if ((c == 'r') && (strncmp(argv[1], "readable", length) == 0)
  536.         && (length >= 5)) {
  537.     if (argc != 3) {
  538.         argv[1] = "readable";
  539.         goto not3Args;
  540.     }
  541.     mode = R_OK;
  542.     checkAccess:
  543.     if (access(fileName, mode) == -1) {
  544.         interp->result = "0";
  545.     } else {
  546.         interp->result = "1";
  547.     }
  548.     goto done;
  549.     } else if ((c == 'w') && (strncmp(argv[1], "writable", length) == 0)) {
  550.     if (argc != 3) {
  551.         argv[1] = "writable";
  552.         goto not3Args;
  553.     }
  554.     mode = W_OK;
  555.     goto checkAccess;
  556.     } else if ((c == 'e') && (strncmp(argv[1], "executable", length) == 0)
  557.         && (length >= 3)) {
  558.     if (argc != 3) {
  559.         argv[1] = "executable";
  560.         goto not3Args;
  561.     }
  562.     mode = X_OK;
  563.     goto checkAccess;
  564.     } else if ((c == 'e') && (strncmp(argv[1], "exists", length) == 0)
  565.         && (length >= 3)) {
  566.     if (argc != 3) {
  567.         argv[1] = "exists";
  568.         goto not3Args;
  569.     }
  570.     mode = F_OK;
  571.     goto checkAccess;
  572.     }
  573.  
  574.     /*
  575.      * Lastly, check stuff that requires the file to be stat-ed.
  576.      */
  577.  
  578.     if ((c == 'a') && (strncmp(argv[1], "atime", length) == 0)) {
  579.     if (argc != 3) {
  580.         argv[1] = "atime";
  581.         goto not3Args;
  582.     }
  583.     if (stat(fileName, &statBuf) == -1) {
  584.         goto badStat;
  585.     }
  586.     sprintf(interp->result, "%ld", statBuf.st_atime);
  587.     goto done;
  588.     } else if ((c == 'i') && (strncmp(argv[1], "isdirectory", length) == 0)
  589.         && (length >= 3)) {
  590.     if (argc != 3) {
  591.         argv[1] = "isdirectory";
  592.         goto not3Args;
  593.     }
  594.     statOp = 2;
  595.     } else if ((c == 'i') && (strncmp(argv[1], "isfile", length) == 0)
  596.         && (length >= 3)) {
  597.     if (argc != 3) {
  598.         argv[1] = "isfile";
  599.         goto not3Args;
  600.     }
  601.     statOp = 1;
  602.     } else if ((c == 'l') && (strncmp(argv[1], "lstat", length) == 0)) {
  603.     if (argc != 4) {
  604.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  605.             " lstat name varName\"", (char *) NULL);
  606.         result = TCL_ERROR;
  607.         goto done;
  608.     }
  609.  
  610.     if (lstat(fileName, &statBuf) == -1) {
  611.         Tcl_AppendResult(interp, "couldn't lstat \"", argv[2],
  612.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  613.         result = TCL_ERROR;
  614.         goto done;
  615.     }
  616.     result = StoreStatData(interp, argv[3], &statBuf);
  617.     goto done;
  618.     } else if ((c == 'm') && (strncmp(argv[1], "mtime", length) == 0)) {
  619.     if (argc != 3) {
  620.         argv[1] = "mtime";
  621.         goto not3Args;
  622.     }
  623.     if (stat(fileName, &statBuf) == -1) {
  624.         goto badStat;
  625.     }
  626.     sprintf(interp->result, "%ld", statBuf.st_mtime);
  627.     goto done;
  628.     } else if ((c == 'o') && (strncmp(argv[1], "owned", length) == 0)) {
  629.     if (argc != 3) {
  630.         argv[1] = "owned";
  631.         goto not3Args;
  632.     }
  633.     statOp = 0;
  634.     } else if ((c == 'r') && (strncmp(argv[1], "readlink", length) == 0)
  635.         && (length >= 5)) {
  636.     char linkValue[MAXPATHLEN+1];
  637.     int linkLength;
  638.  
  639.     if (argc != 3) {
  640.         argv[1] = "readlink";
  641.         goto not3Args;
  642.     }
  643.  
  644.     /*
  645.      * If S_IFLNK isn't defined it means that the machine doesn't
  646.      * support symbolic links, so the file can't possibly be a
  647.      * symbolic link.  Generate an EINVAL error, which is what
  648.      * happens on machines that do support symbolic links when
  649.      * you invoke readlink on a file that isn't a symbolic link.
  650.      */
  651.  
  652. #ifndef S_IFLNK
  653.     linkLength = -1;
  654.     errno = EINVAL;
  655. #else
  656.     linkLength = readlink(fileName, linkValue, sizeof(linkValue) - 1);
  657. #endif /* S_IFLNK */
  658.     if (linkLength == -1) {
  659.         Tcl_AppendResult(interp, "couldn't readlink \"", argv[2],
  660.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  661.         result = TCL_ERROR;
  662.         goto done;
  663.     }
  664.     linkValue[linkLength] = 0;
  665.     Tcl_SetResult(interp, linkValue, TCL_VOLATILE);
  666.     goto done;
  667.     } else if ((c == 's') && (strncmp(argv[1], "size", length) == 0)
  668.         && (length >= 2)) {
  669.     if (argc != 3) {
  670.         argv[1] = "size";
  671.         goto not3Args;
  672.     }
  673.     if (stat(fileName, &statBuf) == -1) {
  674.         goto badStat;
  675.     }
  676.     sprintf(interp->result, "%ld", statBuf.st_size);
  677.     goto done;
  678.     } else if ((c == 's') && (strncmp(argv[1], "stat", length) == 0)
  679.         && (length >= 2)) {
  680.     if (argc != 4) {
  681.         Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  682.             " stat name varName\"", (char *) NULL);
  683.         result = TCL_ERROR;
  684.         goto done;
  685.     }
  686.  
  687.     if (stat(fileName, &statBuf) == -1) {
  688.         badStat:
  689.         Tcl_AppendResult(interp, "couldn't stat \"", argv[2],
  690.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  691.         result = TCL_ERROR;
  692.         goto done;
  693.     }
  694.     result = StoreStatData(interp, argv[3], &statBuf);
  695.     goto done;
  696.     } else if ((c == 't') && (strncmp(argv[1], "type", length) == 0)
  697.         && (length >= 2)) {
  698.     if (argc != 3) {
  699.         argv[1] = "type";
  700.         goto not3Args;
  701.     }
  702.     if (lstat(fileName, &statBuf) == -1) {
  703.         goto badStat;
  704.     }
  705.     interp->result = GetFileType((int) statBuf.st_mode);
  706.     goto done;
  707.     } else {
  708.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  709.         "\": should be atime, dirname, executable, exists, ",
  710.         "extension, isdirectory, isfile, lstat, mtime, owned, ",
  711.         "readable, readlink, ",
  712.         "root, size, stat, tail, type, ",
  713.         "or writable",
  714.         (char *) NULL);
  715.     result = TCL_ERROR;
  716.     goto done;
  717.     }
  718.     if (stat(fileName, &statBuf) == -1) {
  719.     interp->result = "0";
  720.     goto done;
  721.     }
  722.     switch (statOp) {
  723.     case 0:
  724.         mode = (geteuid() == statBuf.st_uid);
  725.         break;
  726.     case 1:
  727.         mode = S_ISREG(statBuf.st_mode);
  728.         break;
  729.     case 2:
  730.         mode = S_ISDIR(statBuf.st_mode);
  731.         break;
  732.     }
  733.     if (mode) {
  734.     interp->result = "1";
  735.     } else {
  736.     interp->result = "0";
  737.     }
  738.  
  739.     done:
  740.     Tcl_DStringFree(&buffer);
  741.     return result;
  742. }
  743.  
  744. /*
  745.  *----------------------------------------------------------------------
  746.  *
  747.  * StoreStatData --
  748.  *
  749.  *    This is a utility procedure that breaks out the fields of a
  750.  *    "stat" structure and stores them in textual form into the
  751.  *    elements of an associative array.
  752.  *
  753.  * Results:
  754.  *    Returns a standard Tcl return value.  If an error occurs then
  755.  *    a message is left in interp->result.
  756.  *
  757.  * Side effects:
  758.  *    Elements of the associative array given by "varName" are modified.
  759.  *
  760.  *----------------------------------------------------------------------
  761.  */
  762.  
  763. static int
  764. StoreStatData(interp, varName, statPtr)
  765.     Tcl_Interp *interp;            /* Interpreter for error reports. */
  766.     char *varName;            /* Name of associative array variable
  767.                      * in which to store stat results. */
  768.     struct stat *statPtr;        /* Pointer to buffer containing
  769.                      * stat data to store in varName. */
  770. {
  771.     char string[30];
  772.  
  773.     sprintf(string, "%d", statPtr->st_dev);
  774.     if (Tcl_SetVar2(interp, varName, "dev", string, TCL_LEAVE_ERR_MSG)
  775.         == NULL) {
  776.     return TCL_ERROR;
  777.     }
  778.     sprintf(string, "%d", statPtr->st_ino);
  779.     if (Tcl_SetVar2(interp, varName, "ino", string, TCL_LEAVE_ERR_MSG)
  780.         == NULL) {
  781.     return TCL_ERROR;
  782.     }
  783.     sprintf(string, "%d", statPtr->st_mode);
  784.     if (Tcl_SetVar2(interp, varName, "mode", string, TCL_LEAVE_ERR_MSG)
  785.         == NULL) {
  786.     return TCL_ERROR;
  787.     }
  788.     sprintf(string, "%d", statPtr->st_nlink);
  789.     if (Tcl_SetVar2(interp, varName, "nlink", string, TCL_LEAVE_ERR_MSG)
  790.         == NULL) {
  791.     return TCL_ERROR;
  792.     }
  793.     sprintf(string, "%d", statPtr->st_uid);
  794.     if (Tcl_SetVar2(interp, varName, "uid", string, TCL_LEAVE_ERR_MSG)
  795.         == NULL) {
  796.     return TCL_ERROR;
  797.     }
  798.     sprintf(string, "%d", statPtr->st_gid);
  799.     if (Tcl_SetVar2(interp, varName, "gid", string, TCL_LEAVE_ERR_MSG)
  800.         == NULL) {
  801.     return TCL_ERROR;
  802.     }
  803.     sprintf(string, "%ld", statPtr->st_size);
  804.     if (Tcl_SetVar2(interp, varName, "size", string, TCL_LEAVE_ERR_MSG)
  805.         == NULL) {
  806.     return TCL_ERROR;
  807.     }
  808.     sprintf(string, "%ld", statPtr->st_atime);
  809.     if (Tcl_SetVar2(interp, varName, "atime", string, TCL_LEAVE_ERR_MSG)
  810.         == NULL) {
  811.     return TCL_ERROR;
  812.     }
  813.     sprintf(string, "%ld", statPtr->st_mtime);
  814.     if (Tcl_SetVar2(interp, varName, "mtime", string, TCL_LEAVE_ERR_MSG)
  815.         == NULL) {
  816.     return TCL_ERROR;
  817.     }
  818.     sprintf(string, "%ld", statPtr->st_ctime);
  819.     if (Tcl_SetVar2(interp, varName, "ctime", string, TCL_LEAVE_ERR_MSG)
  820.         == NULL) {
  821.     return TCL_ERROR;
  822.     }
  823.     if (Tcl_SetVar2(interp, varName, "type",
  824.         GetFileType((int) statPtr->st_mode), TCL_LEAVE_ERR_MSG) == NULL) {
  825.     return TCL_ERROR;
  826.     }
  827.     return TCL_OK;
  828. }
  829.  
  830. /*
  831.  *----------------------------------------------------------------------
  832.  *
  833.  * GetFileType --
  834.  *
  835.  *    Given a mode word, returns a string identifying the type of a
  836.  *    file.
  837.  *
  838.  * Results:
  839.  *    A static text string giving the file type from mode.
  840.  *
  841.  * Side effects:
  842.  *    None.
  843.  *
  844.  *----------------------------------------------------------------------
  845.  */
  846.  
  847. static char *
  848. GetFileType(mode)
  849.     int mode;
  850. {
  851.     if (S_ISREG(mode)) {
  852.     return "file";
  853.     } else if (S_ISDIR(mode)) {
  854.     return "directory";
  855.     } else if (S_ISCHR(mode)) {
  856.     return "characterSpecial";
  857.     } else if (S_ISBLK(mode)) {
  858.     return "blockSpecial";
  859.     } else if (S_ISFIFO(mode)) {
  860.     return "fifo";
  861.     } else if (S_ISLNK(mode)) {
  862.     return "link";
  863.     } else if (S_ISSOCK(mode)) {
  864.     return "socket";
  865.     }
  866.     return "unknown";
  867. }
  868.  
  869. /*
  870.  *----------------------------------------------------------------------
  871.  *
  872.  * Tcl_FlushCmd --
  873.  *
  874.  *    This procedure is invoked to process the "flush" Tcl command.
  875.  *    See the user documentation for details on what it does.
  876.  *
  877.  * Results:
  878.  *    A standard Tcl result.
  879.  *
  880.  * Side effects:
  881.  *    See the user documentation.
  882.  *
  883.  *----------------------------------------------------------------------
  884.  */
  885.  
  886.     /* ARGSUSED */
  887. int
  888. Tcl_FlushCmd(notUsed, interp, argc, argv)
  889.     ClientData notUsed;            /* Not used. */
  890.     Tcl_Interp *interp;            /* Current interpreter. */
  891.     int argc;                /* Number of arguments. */
  892.     char **argv;            /* Argument strings. */
  893. {
  894.     FILE *f;
  895.  
  896.     if (argc != 2) {
  897.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  898.         " fileId\"", (char *) NULL);
  899.     return TCL_ERROR;
  900.     }
  901.     if (Tcl_GetOpenFile(interp, argv[1], 1, 1, &f) != TCL_OK) {
  902.     return TCL_ERROR;
  903.     }
  904.     if (fflush(f) == EOF) {
  905.     Tcl_AppendResult(interp, "error flushing \"", argv[1],
  906.         "\": ", Tcl_PosixError(interp), (char *) NULL);
  907.     clearerr(f);
  908.     return TCL_ERROR;
  909.     }
  910.     return TCL_OK;
  911. }
  912.  
  913. /*
  914.  *----------------------------------------------------------------------
  915.  *
  916.  * Tcl_GetsCmd --
  917.  *
  918.  *    This procedure is invoked to process the "gets" Tcl command.
  919.  *    See the user documentation for details on what it does.
  920.  *
  921.  * Results:
  922.  *    A standard Tcl result.
  923.  *
  924.  * Side effects:
  925.  *    See the user documentation.
  926.  *
  927.  *----------------------------------------------------------------------
  928.  */
  929.  
  930.     /* ARGSUSED */
  931. int
  932. Tcl_GetsCmd(notUsed, interp, argc, argv)
  933.     ClientData notUsed;            /* Not used. */
  934.     Tcl_Interp *interp;            /* Current interpreter. */
  935.     int argc;                /* Number of arguments. */
  936.     char **argv;            /* Argument strings. */
  937. {
  938. #   define BUF_SIZE 200
  939.     char buffer[BUF_SIZE+1];
  940.     int totalCount, done, flags;
  941.     FILE *f;
  942.  
  943.     if ((argc != 2) && (argc != 3)) {
  944.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  945.         " fileId ?varName?\"", (char *) NULL);
  946.     return TCL_ERROR;
  947.     }
  948.     if (Tcl_GetOpenFile(interp, argv[1], 0, 1, &f) != TCL_OK) {
  949.     return TCL_ERROR;
  950.     }
  951.  
  952.     /*
  953.      * We can't predict how large a line will be, so read it in
  954.      * pieces, appending to the current result or to a variable.
  955.      */
  956.  
  957.     totalCount = 0;
  958.     done = 0;
  959.     flags = 0;
  960.     while (!done) {
  961.     register int c, count;
  962.     register char *p;
  963.  
  964.     for (p = buffer, count = 0; count < BUF_SIZE-1; count++, p++) {
  965.         c = getc(f);
  966.         if (c == EOF) {
  967.         if (ferror(f)) {
  968.             Tcl_ResetResult(interp);
  969.             Tcl_AppendResult(interp, "error reading \"", argv[1],
  970.                 "\": ", Tcl_PosixError(interp), (char *) NULL);
  971.             clearerr(f);
  972.             return TCL_ERROR;
  973.         } else if (feof(f)) {
  974.             if ((totalCount == 0) && (count == 0)) {
  975.             totalCount = -1;
  976.             }
  977.             done = 1;
  978.             break;
  979.         }
  980.         }
  981.         if (c == '\n') {
  982.         done = 1;
  983.         break;
  984.         }
  985.         *p = c;
  986.     }
  987.     *p = 0;
  988.     if (argc == 2) {
  989.         Tcl_AppendResult(interp, buffer, (char *) NULL);
  990.     } else {
  991.         if (Tcl_SetVar(interp, argv[2], buffer, flags|TCL_LEAVE_ERR_MSG)
  992.             == NULL) {
  993.         return TCL_ERROR;
  994.         }
  995.         flags = TCL_APPEND_VALUE;
  996.     }
  997.     totalCount += count;
  998.     }
  999.  
  1000.     if (argc == 3) {
  1001.     sprintf(interp->result, "%d", totalCount);
  1002.     }
  1003.     return TCL_OK;
  1004. }
  1005.  
  1006. /*
  1007.  *----------------------------------------------------------------------
  1008.  *
  1009.  * Tcl_OpenCmd --
  1010.  *
  1011.  *    This procedure is invoked to process the "open" Tcl command.
  1012.  *    See the user documentation for details on what it does.
  1013.  *
  1014.  * Results:
  1015.  *    A standard Tcl result.
  1016.  *
  1017.  * Side effects:
  1018.  *    See the user documentation.
  1019.  *
  1020.  *----------------------------------------------------------------------
  1021.  */
  1022.  
  1023.     /* ARGSUSED */
  1024. int
  1025. Tcl_OpenCmd(notUsed, interp, argc, argv)
  1026.     ClientData notUsed;            /* Not used. */
  1027.     Tcl_Interp *interp;            /* Current interpreter. */
  1028.     int argc;                /* Number of arguments. */
  1029.     char **argv;            /* Argument strings. */
  1030. {
  1031.     Interp *iPtr = (Interp *) interp;
  1032.     int pipeline, fd, mode, prot, readWrite, readable, writable;
  1033.     char *access;
  1034.     FILE *f, *f2;
  1035.  
  1036.     if ((argc < 2) || (argc > 4)) {
  1037.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1038.         " filename ?access? ?permissions?\"", (char *) NULL);
  1039.     return TCL_ERROR;
  1040.     }
  1041.     prot = 0666;
  1042.     if (argc == 2) {
  1043.     mode = O_RDONLY;
  1044.     access = "r";
  1045.     } else {
  1046.     access = GetOpenMode(interp, argv[2], &mode);
  1047.     if (access == NULL) {
  1048.         return TCL_ERROR;
  1049.     }
  1050.     if (argc == 4) {
  1051.         if (Tcl_GetInt(interp, argv[3], &prot) != TCL_OK) {
  1052.         return TCL_ERROR;
  1053.         }
  1054.     }
  1055.     }
  1056.  
  1057.     f = f2 = NULL;
  1058.     readWrite = mode & (O_RDWR|O_RDONLY|O_WRONLY);
  1059.     if (readWrite == O_RDONLY) {
  1060.     readable = 1;
  1061.     writable = 0;
  1062.     } else if (readWrite == O_WRONLY) {
  1063.     readable = 0;
  1064.     writable = 1;
  1065.     } else {
  1066.     readable = 1;
  1067.     writable = 1;
  1068.     }
  1069.  
  1070.     pipeline = 0;
  1071.     if (argv[1][0] == '|') {
  1072.     pipeline = 1;
  1073.     }
  1074.  
  1075.     /*
  1076.      * Open the file or create a process pipeline.
  1077.      */
  1078.  
  1079.     if (!pipeline) {
  1080.     char *fileName;
  1081.     Tcl_DString buffer;
  1082.  
  1083.     fileName = Tcl_TildeSubst(interp, argv[1], &buffer);
  1084.     if (fileName == NULL) {
  1085.         return TCL_ERROR;
  1086.     }
  1087.     fd = open(fileName, mode, prot);
  1088.     Tcl_DStringFree(&buffer);
  1089.     if (fd < 0) {
  1090.         Tcl_AppendResult(interp, "couldn't open \"", argv[1],
  1091.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  1092.         return TCL_ERROR;
  1093.     }
  1094.     f = fdopen(fd, access);
  1095.     if (f == NULL) {
  1096.         close(fd);
  1097.         return TCL_ERROR;
  1098.     }
  1099.     Tcl_EnterFile(interp, f, readable, writable);
  1100.     } else {
  1101.     int *inPipePtr, *outPipePtr;
  1102.     int cmdArgc, inPipe, outPipe, numPids, *pidPtr, errorId;
  1103.     char **cmdArgv;
  1104.     OpenFile *oFilePtr;
  1105.  
  1106.     if (Tcl_SplitList(interp, argv[1]+1, &cmdArgc, &cmdArgv) != TCL_OK) {
  1107.         return TCL_ERROR;
  1108.     }
  1109.     inPipePtr = (writable) ? &inPipe : NULL;
  1110.     outPipePtr = (readable) ? &outPipe : NULL;
  1111.     inPipe = outPipe = errorId = -1;
  1112.     numPids = Tcl_CreatePipeline(interp, cmdArgc, cmdArgv,
  1113.         &pidPtr, inPipePtr, outPipePtr, &errorId);
  1114.     ckfree((char *) cmdArgv);
  1115.     if (numPids < 0) {
  1116.         pipelineError:
  1117.         if (f != NULL) {
  1118.         fclose(f);
  1119.         }
  1120.         if (f2 != NULL) {
  1121.         fclose(f2);
  1122.         }
  1123.         if (numPids > 0) {
  1124.         Tcl_DetachPids(numPids, pidPtr);
  1125.         ckfree((char *) pidPtr);
  1126.         }
  1127.         if (errorId != -1) {
  1128.         close(errorId);
  1129.         }
  1130.         return TCL_ERROR;
  1131.     }
  1132.     if (readable) {
  1133.         if (outPipe == -1) {
  1134.         if (inPipe != -1) {
  1135.             close(inPipe);
  1136.         }
  1137.         Tcl_AppendResult(interp, "can't read output from command:",
  1138.             " standard output was redirected", (char *) NULL);
  1139.         goto pipelineError;
  1140.         }
  1141.         f = fdopen(outPipe, "r");
  1142.     }
  1143.     if (writable) {
  1144.         if (inPipe == -1) {
  1145.         Tcl_AppendResult(interp, "can't write input to command:",
  1146.             " standard input was redirected", (char *) NULL);
  1147.         goto pipelineError;
  1148.         }
  1149.         if (f != NULL) {
  1150.         f2 = fdopen(inPipe, "w");
  1151.         } else {
  1152.         f = fdopen(inPipe, "w");
  1153.         }
  1154.     }
  1155.     Tcl_EnterFile(interp, f, readable, writable);
  1156.     oFilePtr = iPtr->oFilePtrArray[fileno(f)];
  1157.     oFilePtr->f2 = f2;
  1158.     oFilePtr->numPids = numPids;
  1159.     oFilePtr->pidPtr = pidPtr;
  1160.     oFilePtr->errorId = errorId;
  1161.     }
  1162.     return TCL_OK;
  1163. }
  1164.  
  1165. /*
  1166.  *----------------------------------------------------------------------
  1167.  *
  1168.  * GetOpenMode --
  1169.  *
  1170.  *    description.
  1171.  *
  1172.  * Results:
  1173.  *    Normally, sets *modePtr to an access mode for passing to "open",
  1174.  *    and returns a string that can be used as the access mode in a
  1175.  *    subsequent call to "fdopen".  If an error occurs, then returns
  1176.  *    NULL and sets interp->result to an error message.
  1177.  *
  1178.  * Side effects:
  1179.  *    None.
  1180.  *
  1181.  * Special note:
  1182.  *    This code is based on a prototype implementation contributed
  1183.  *    by Mark Diekhans.
  1184.  *
  1185.  *----------------------------------------------------------------------
  1186.  */
  1187.  
  1188. static char *
  1189. GetOpenMode(interp, string, modePtr)
  1190.     Tcl_Interp *interp;            /* Interpreter to use for error
  1191.                      * reporting. */
  1192.     char *string;            /* Mode string, e.g. "r+" or
  1193.                      * "RDONLY CREAT". */
  1194.     int *modePtr;            /* Where to store mode corresponding
  1195.                      * to string. */
  1196. {
  1197.     int mode, modeArgc, c, i, gotRW;
  1198.     char **modeArgv, *flag;
  1199. #define RW_MODES (O_RDONLY|O_WRONLY|O_RDWR)
  1200.  
  1201.     /*
  1202.      * Check for the simpler fopen-like access modes (e.g. "r").  They
  1203.      * are distinguished from the POSIX access modes by the presence
  1204.      * of a lower-case first letter.
  1205.      */
  1206.  
  1207.     mode = 0;
  1208.     if (islower(string[0])) {
  1209.     switch (string[0]) {
  1210.         case 'r':
  1211.         mode = O_RDONLY;
  1212.         break;
  1213.         case 'w':
  1214.         mode = O_WRONLY|O_CREAT|O_TRUNC;
  1215.         break;
  1216.         case 'a':
  1217.         mode = O_WRONLY|O_CREAT|O_APPEND;
  1218.         break;
  1219.         default:
  1220.         error:
  1221.         Tcl_AppendResult(interp,
  1222.             "illegal access mode \"", string, "\"", (char *) NULL);
  1223.         return NULL;
  1224.     }
  1225.     if (string[1] == '+') {
  1226.         mode &= ~(O_RDONLY|O_WRONLY);
  1227.         mode |= O_RDWR;
  1228.         if (string[2] != 0) {
  1229.         goto error;
  1230.         }
  1231.     } else if (string[1] != 0) {
  1232.         goto error;
  1233.     }
  1234.     *modePtr = mode;
  1235.     return string;
  1236.     }
  1237.  
  1238.     /*
  1239.      * The access modes are specified using a list of POSIX modes
  1240.      * such as O_CREAT.
  1241.      */
  1242.  
  1243.     if (Tcl_SplitList(interp, string, &modeArgc, &modeArgv) != TCL_OK) {
  1244.     Tcl_AddErrorInfo(interp, "\n    while processing open access modes \"");
  1245.     Tcl_AddErrorInfo(interp, string);
  1246.     Tcl_AddErrorInfo(interp, "\"");
  1247.     return NULL;
  1248.     }
  1249.     gotRW = 0;
  1250.     for (i = 0; i < modeArgc; i++) {
  1251.     flag = modeArgv[i];
  1252.     c = flag[0];
  1253.     if ((c == 'R') && (strcmp(flag, "RDONLY") == 0)) {
  1254.         mode = (mode & ~RW_MODES) | O_RDONLY;
  1255.         gotRW = 1;
  1256.     } else if ((c == 'W') && (strcmp(flag, "WRONLY") == 0)) {
  1257.         mode = (mode & ~RW_MODES) | O_WRONLY;
  1258.         gotRW = 1;
  1259.     } else if ((c == 'R') && (strcmp(flag, "RDWR") == 0)) {
  1260.         mode = (mode & ~RW_MODES) | O_RDWR;
  1261.         gotRW = 1;
  1262.     } else if ((c == 'A') && (strcmp(flag, "APPEND") == 0)) {
  1263.         mode |= O_APPEND;
  1264.     } else if ((c == 'C') && (strcmp(flag, "CREAT") == 0)) {
  1265.         mode |= O_CREAT;
  1266.     } else if ((c == 'E') && (strcmp(flag, "EXCL") == 0)) {
  1267.         mode |= O_EXCL;
  1268.     } else if ((c == 'N') && (strcmp(flag, "NOCTTY") == 0)) {
  1269. #ifdef O_NOCTTY
  1270.         mode |= O_NOCTTY;
  1271. #else
  1272.         ckfree((char *) modeArgv);
  1273.         Tcl_AppendResult(interp, "access mode \"", flag,
  1274.             "\" not supported by this system", (char *) NULL);
  1275.         return NULL;
  1276. #endif
  1277.     } else if ((c == 'N') && (strcmp(flag, "NONBLOCK") == 0)) {
  1278. #ifdef O_NONBLOCK
  1279.         mode |= O_NONBLOCK;
  1280. #else
  1281.         mode |= O_NDELAY;
  1282. #endif
  1283.     } else if ((c == 'T') && (strcmp(flag, "TRUNC") == 0)) {
  1284.         mode |= O_TRUNC;
  1285.     } else {
  1286.         ckfree((char *) modeArgv);
  1287.         Tcl_AppendResult(interp, "invalid access mode \"", flag,
  1288.             "\": must be RDONLY, WRONLY, RDWR, APPEND, CREAT",
  1289.             " EXCL, NOCTTY, NONBLOCK, or TRUNC", (char *) NULL);
  1290.         return NULL;
  1291.     }
  1292.     }
  1293.     ckfree((char *) modeArgv);
  1294.     if (!gotRW) {
  1295.     Tcl_AppendResult(interp, "access mode must include either",
  1296.         " RDONLY, WRONLY, or RDWR", (char *) NULL);
  1297.     return NULL;
  1298.     }
  1299.     *modePtr = mode;
  1300.  
  1301.     /*
  1302.      * The calculation of fdopen access mode below isn't really correct,
  1303.      * but it doesn't have to be.  All it has to do is to disinguish
  1304.      * read and write permissions, plus indicate append mode.
  1305.      */
  1306.  
  1307.     i = mode & RW_MODES;
  1308.     if (i == O_RDONLY) {
  1309.     return "r";
  1310.     }
  1311.     if (mode & O_APPEND) {
  1312.     if (i == O_WRONLY) {
  1313.         return "a";
  1314.     } else {
  1315.         return "a+";
  1316.     }
  1317.     }
  1318.     if (i == O_WRONLY) {
  1319.     return "w";
  1320.     }
  1321.     return "r+";
  1322. }
  1323.  
  1324. /*
  1325.  *----------------------------------------------------------------------
  1326.  *
  1327.  * Tcl_PidCmd --
  1328.  *
  1329.  *    This procedure is invoked to process the "pid" Tcl command.
  1330.  *    See the user documentation for details on what it does.
  1331.  *
  1332.  * Results:
  1333.  *    A standard Tcl result.
  1334.  *
  1335.  * Side effects:
  1336.  *    See the user documentation.
  1337.  *
  1338.  *----------------------------------------------------------------------
  1339.  */
  1340.  
  1341.     /* ARGSUSED */
  1342. int
  1343. Tcl_PidCmd(dummy, interp, argc, argv)
  1344.     ClientData dummy;            /* Not used. */
  1345.     Tcl_Interp *interp;            /* Current interpreter. */
  1346.     int argc;                /* Number of arguments. */
  1347.     char **argv;            /* Argument strings. */
  1348. {
  1349.     FILE *f;
  1350.     OpenFile *oFilePtr;
  1351.     int i;
  1352.     char string[50];
  1353.  
  1354.     if (argc > 2) {
  1355.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  1356.         argv[0], " ?fileId?\"", (char *) NULL);
  1357.     return TCL_ERROR;
  1358.     }
  1359.     if (argc == 1) {
  1360.     sprintf(interp->result, "%d", getpid());
  1361.     } else {
  1362.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  1363.         return TCL_ERROR;
  1364.     }
  1365.     oFilePtr = ((Interp *) interp)->oFilePtrArray[fileno(f)];
  1366.     for (i = 0; i < oFilePtr->numPids; i++) {
  1367.         sprintf(string, "%d", oFilePtr->pidPtr[i]);
  1368.         Tcl_AppendElement(interp, string);
  1369.     }
  1370.     }
  1371.     return TCL_OK;
  1372. }
  1373.  
  1374. /*
  1375.  *----------------------------------------------------------------------
  1376.  *
  1377.  * Tcl_PutsCmd --
  1378.  *
  1379.  *    This procedure is invoked to process the "puts" Tcl command.
  1380.  *    See the user documentation for details on what it does.
  1381.  *
  1382.  * Results:
  1383.  *    A standard Tcl result.
  1384.  *
  1385.  * Side effects:
  1386.  *    See the user documentation.
  1387.  *
  1388.  *----------------------------------------------------------------------
  1389.  */
  1390.  
  1391.     /* ARGSUSED */
  1392. int
  1393. Tcl_PutsCmd(dummy, interp, argc, argv)
  1394.     ClientData dummy;            /* Not used. */
  1395.     Tcl_Interp *interp;            /* Current interpreter. */
  1396.     int argc;                /* Number of arguments. */
  1397.     char **argv;            /* Argument strings. */
  1398. {
  1399.     FILE *f;
  1400.     int i, newline;
  1401.     char *fileId;
  1402.  
  1403.     i = 1;
  1404.     newline = 1;
  1405.     if ((argc >= 2) && (strcmp(argv[1], "-nonewline") == 0)) {
  1406.     newline = 0;
  1407.     i++;
  1408.     }
  1409.     if ((i < (argc-3)) || (i >= argc)) {
  1410.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1411.         "\" ?-nonewline? ?fileId? string", (char *) NULL);
  1412.     return TCL_ERROR;
  1413.     }
  1414.  
  1415.     /*
  1416.      * The code below provides backwards compatibility with an old
  1417.      * form of the command that is no longer recommended or documented.
  1418.      */
  1419.  
  1420.     if (i == (argc-3)) {
  1421.     if (strncmp(argv[i+2], "nonewline", strlen(argv[i+2])) != 0) {
  1422.         Tcl_AppendResult(interp, "bad argument \"", argv[i+2],
  1423.             "\": should be \"nonewline\"", (char *) NULL);
  1424.         return TCL_ERROR;
  1425.     }
  1426.     newline = 0;
  1427.     }
  1428.     if (i == (argc-1)) {
  1429.     fileId = "stdout";
  1430.     } else {
  1431.     fileId = argv[i];
  1432.     i++;
  1433.     }
  1434.  
  1435.     if (Tcl_GetOpenFile(interp, fileId, 1, 1, &f) != TCL_OK) {
  1436.     return TCL_ERROR;
  1437.     }
  1438.  
  1439.     fputs(argv[i], f);
  1440.     if (newline) {
  1441.     fputc('\n', f);
  1442.     }
  1443.     if (ferror(f)) {
  1444.     Tcl_AppendResult(interp, "error writing \"", fileId,
  1445.         "\": ", Tcl_PosixError(interp), (char *) NULL);
  1446.     clearerr(f);
  1447.     return TCL_ERROR;
  1448.     }
  1449.     return TCL_OK;
  1450. }
  1451.  
  1452. /*
  1453.  *----------------------------------------------------------------------
  1454.  *
  1455.  * Tcl_PwdCmd --
  1456.  *
  1457.  *    This procedure is invoked to process the "pwd" Tcl command.
  1458.  *    See the user documentation for details on what it does.
  1459.  *
  1460.  * Results:
  1461.  *    A standard Tcl result.
  1462.  *
  1463.  * Side effects:
  1464.  *    See the user documentation.
  1465.  *
  1466.  *----------------------------------------------------------------------
  1467.  */
  1468.  
  1469.     /* ARGSUSED */
  1470. int
  1471. Tcl_PwdCmd(dummy, interp, argc, argv)
  1472.     ClientData dummy;            /* Not used. */
  1473.     Tcl_Interp *interp;            /* Current interpreter. */
  1474.     int argc;                /* Number of arguments. */
  1475.     char **argv;            /* Argument strings. */
  1476. {
  1477.     char buffer[MAXPATHLEN+1];
  1478.  
  1479.     if (argc != 1) {
  1480.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  1481.         argv[0], "\"", (char *) NULL);
  1482.     return TCL_ERROR;
  1483.     }
  1484.     if (currentDir == NULL) {
  1485.     if (getcwd(buffer, MAXPATHLEN+1) == NULL) {
  1486.         if (errno == ERANGE) {
  1487.         interp->result = "working directory name is too long";
  1488.         } else {
  1489.         Tcl_AppendResult(interp,
  1490.             "error getting working directory name: ",
  1491.             Tcl_PosixError(interp), (char *) NULL);
  1492.         }
  1493.         return TCL_ERROR;
  1494.     }
  1495.     currentDir = (char *) ckalloc((unsigned) (strlen(buffer) + 1));
  1496.     strcpy(currentDir, buffer);
  1497.     }
  1498.     interp->result = currentDir;
  1499.     return TCL_OK;
  1500. }
  1501.  
  1502. /*
  1503.  *----------------------------------------------------------------------
  1504.  *
  1505.  * Tcl_ReadCmd --
  1506.  *
  1507.  *    This procedure is invoked to process the "read" Tcl command.
  1508.  *    See the user documentation for details on what it does.
  1509.  *
  1510.  * Results:
  1511.  *    A standard Tcl result.
  1512.  *
  1513.  * Side effects:
  1514.  *    See the user documentation.
  1515.  *
  1516.  *----------------------------------------------------------------------
  1517.  */
  1518.  
  1519.     /* ARGSUSED */
  1520. int
  1521. Tcl_ReadCmd(dummy, interp, argc, argv)
  1522.     ClientData dummy;            /* Not used. */
  1523.     Tcl_Interp *interp;            /* Current interpreter. */
  1524.     int argc;                /* Number of arguments. */
  1525.     char **argv;            /* Argument strings. */
  1526. {
  1527.     int bytesLeft, bytesRead, count;
  1528. #define READ_BUF_SIZE 4096
  1529.     char buffer[READ_BUF_SIZE+1];
  1530.     int newline, i;
  1531.     FILE *f;
  1532.  
  1533.     if ((argc != 2) && (argc != 3)) {
  1534.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1535.         " fileId ?numBytes?\" or \"", argv[0],
  1536.         " ?-nonewline? fileId\"", (char *) NULL);
  1537.     return TCL_ERROR;
  1538.     }
  1539.     i = 1;
  1540.     newline = 1;
  1541.     if ((argc == 3) && (strcmp(argv[1], "-nonewline") == 0)) {
  1542.     newline = 0;
  1543.     i++;
  1544.     }
  1545.     if (Tcl_GetOpenFile(interp, argv[i], 0, 1, &f) != TCL_OK) {
  1546.     return TCL_ERROR;
  1547.     }
  1548.  
  1549.     /*
  1550.      * Compute how many bytes to read, and see whether the final
  1551.      * newline should be dropped.
  1552.      */
  1553.  
  1554.     if ((argc >= (i + 2)) && isdigit(argv[i+1][0])) {
  1555.     if (Tcl_GetInt(interp, argv[i+1], &bytesLeft) != TCL_OK) {
  1556.         return TCL_ERROR;
  1557.     }
  1558.     } else {
  1559.     bytesLeft = 1<<30;
  1560.  
  1561.     /*
  1562.      * The code below provides backward compatibility for an
  1563.      * archaic earlier version of this command.
  1564.      */
  1565.  
  1566.     if (argc >= (i + 2)) {
  1567.         if (strncmp(argv[i+1], "nonewline", strlen(argv[i+1])) == 0) {
  1568.         newline = 0;
  1569.         } else {
  1570.         Tcl_AppendResult(interp, "bad argument \"", argv[i+1],
  1571.             "\": should be \"nonewline\"", (char *) NULL);
  1572.         return TCL_ERROR;
  1573.         }
  1574.     }
  1575.     }
  1576.  
  1577.     /*
  1578.      * Read the file in one or more chunks.
  1579.      */
  1580.  
  1581.     bytesRead = 0;
  1582.     while (bytesLeft > 0) {
  1583.     count = READ_BUF_SIZE;
  1584.     if (bytesLeft < READ_BUF_SIZE) {
  1585.         count = bytesLeft;
  1586.     }
  1587.     count = fread(buffer, 1, count, f);
  1588.     if (ferror(f)) {
  1589.         Tcl_ResetResult(interp);
  1590.         Tcl_AppendResult(interp, "error reading \"", argv[i],
  1591.             "\": ", Tcl_PosixError(interp), (char *) NULL);
  1592.         clearerr(f);
  1593.         return TCL_ERROR;
  1594.     }
  1595.     if (count == 0) {
  1596.         break;
  1597.     }
  1598.     buffer[count] = 0;
  1599.     Tcl_AppendResult(interp, buffer, (char *) NULL);
  1600.     bytesLeft -= count;
  1601.     bytesRead += count;
  1602.     }
  1603.     if ((newline == 0) && (bytesRead > 0)
  1604.         && (interp->result[bytesRead-1] == '\n')) {
  1605.     interp->result[bytesRead-1] = 0;
  1606.     }
  1607.     return TCL_OK;
  1608. }
  1609.  
  1610. /*
  1611.  *----------------------------------------------------------------------
  1612.  *
  1613.  * Tcl_SeekCmd --
  1614.  *
  1615.  *    This procedure is invoked to process the "seek" Tcl command.
  1616.  *    See the user documentation for details on what it does.
  1617.  *
  1618.  * Results:
  1619.  *    A standard Tcl result.
  1620.  *
  1621.  * Side effects:
  1622.  *    See the user documentation.
  1623.  *
  1624.  *----------------------------------------------------------------------
  1625.  */
  1626.  
  1627.     /* ARGSUSED */
  1628. int
  1629. Tcl_SeekCmd(notUsed, interp, argc, argv)
  1630.     ClientData notUsed;            /* Not used. */
  1631.     Tcl_Interp *interp;            /* Current interpreter. */
  1632.     int argc;                /* Number of arguments. */
  1633.     char **argv;            /* Argument strings. */
  1634. {
  1635.     FILE *f;
  1636.     int offset, mode;
  1637.  
  1638.     if ((argc != 3) && (argc != 4)) {
  1639.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1640.         " fileId offset ?origin?\"", (char *) NULL);
  1641.     return TCL_ERROR;
  1642.     }
  1643.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  1644.     return TCL_ERROR;
  1645.     }
  1646.     if (Tcl_GetInt(interp, argv[2], &offset) != TCL_OK) {
  1647.     return TCL_ERROR;
  1648.     }
  1649.     mode = SEEK_SET;
  1650.     if (argc == 4) {
  1651.     int length;
  1652.     char c;
  1653.  
  1654.     length = strlen(argv[3]);
  1655.     c = argv[3][0];
  1656.     if ((c == 's') && (strncmp(argv[3], "start", length) == 0)) {
  1657.         mode = SEEK_SET;
  1658.     } else if ((c == 'c') && (strncmp(argv[3], "current", length) == 0)) {
  1659.         mode = SEEK_CUR;
  1660.     } else if ((c == 'e') && (strncmp(argv[3], "end", length) == 0)) {
  1661.         mode = SEEK_END;
  1662.     } else {
  1663.         Tcl_AppendResult(interp, "bad origin \"", argv[3],
  1664.             "\": should be start, current, or end", (char *) NULL);
  1665.         return TCL_ERROR;
  1666.     }
  1667.     }
  1668.     if (fseek(f, (long) offset, mode) == -1) {
  1669.     Tcl_AppendResult(interp, "error during seek: ",
  1670.         Tcl_PosixError(interp), (char *) NULL);
  1671.     clearerr(f);
  1672.     return TCL_ERROR;
  1673.     }
  1674.  
  1675.     return TCL_OK;
  1676. }
  1677.  
  1678. /*
  1679.  *----------------------------------------------------------------------
  1680.  *
  1681.  * Tcl_SourceCmd --
  1682.  *
  1683.  *    This procedure is invoked to process the "source" Tcl command.
  1684.  *    See the user documentation for details on what it does.
  1685.  *
  1686.  * Results:
  1687.  *    A standard Tcl result.
  1688.  *
  1689.  * Side effects:
  1690.  *    See the user documentation.
  1691.  *
  1692.  *----------------------------------------------------------------------
  1693.  */
  1694.  
  1695.     /* ARGSUSED */
  1696. int
  1697. Tcl_SourceCmd(dummy, interp, argc, argv)
  1698.     ClientData dummy;            /* Not used. */
  1699.     Tcl_Interp *interp;            /* Current interpreter. */
  1700.     int argc;                /* Number of arguments. */
  1701.     char **argv;            /* Argument strings. */
  1702. {
  1703.     if (argc != 2) {
  1704.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1705.         " fileName\"", (char *) NULL);
  1706.     return TCL_ERROR;
  1707.     }
  1708.     return Tcl_EvalFile(interp, argv[1]);
  1709. }
  1710.  
  1711. /*
  1712.  *----------------------------------------------------------------------
  1713.  *
  1714.  * Tcl_TellCmd --
  1715.  *
  1716.  *    This procedure is invoked to process the "tell" Tcl command.
  1717.  *    See the user documentation for details on what it does.
  1718.  *
  1719.  * Results:
  1720.  *    A standard Tcl result.
  1721.  *
  1722.  * Side effects:
  1723.  *    See the user documentation.
  1724.  *
  1725.  *----------------------------------------------------------------------
  1726.  */
  1727.  
  1728.     /* ARGSUSED */
  1729. int
  1730. Tcl_TellCmd(notUsed, interp, argc, argv)
  1731.     ClientData notUsed;            /* Not used. */
  1732.     Tcl_Interp *interp;            /* Current interpreter. */
  1733.     int argc;                /* Number of arguments. */
  1734.     char **argv;            /* Argument strings. */
  1735. {
  1736.     FILE *f;
  1737.  
  1738.     if (argc != 2) {
  1739.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1740.         " fileId\"", (char *) NULL);
  1741.     return TCL_ERROR;
  1742.     }
  1743.     if (Tcl_GetOpenFile(interp, argv[1], 0, 0, &f) != TCL_OK) {
  1744.     return TCL_ERROR;
  1745.     }
  1746.     sprintf(interp->result, "%d", ftell(f));
  1747.     return TCL_OK;
  1748. }
  1749.  
  1750. /*
  1751.  *----------------------------------------------------------------------
  1752.  *
  1753.  * Tcl_TimeCmd --
  1754.  *
  1755.  *    This procedure is invoked to process the "time" Tcl command.
  1756.  *    See the user documentation for details on what it does.
  1757.  *
  1758.  * Results:
  1759.  *    A standard Tcl result.
  1760.  *
  1761.  * Side effects:
  1762.  *    See the user documentation.
  1763.  *
  1764.  *----------------------------------------------------------------------
  1765.  */
  1766.  
  1767.     /* ARGSUSED */
  1768. int
  1769. Tcl_TimeCmd(dummy, interp, argc, argv)
  1770.     ClientData dummy;            /* Not used. */
  1771.     Tcl_Interp *interp;            /* Current interpreter. */
  1772.     int argc;                /* Number of arguments. */
  1773.     char **argv;            /* Argument strings. */
  1774. {
  1775.     int count, i, result;
  1776.     double timePer;
  1777. #if NO_GETTOD
  1778.     struct tms dummy2;
  1779.     long start, stop;
  1780. #else
  1781.     struct timeval start, stop;
  1782.     struct timezone tz;
  1783.     int micros;
  1784. #endif
  1785.  
  1786.     if (argc == 2) {
  1787.     count = 1;
  1788.     } else if (argc == 3) {
  1789.     if (Tcl_GetInt(interp, argv[2], &count) != TCL_OK) {
  1790.         return TCL_ERROR;
  1791.     }
  1792.     } else {
  1793.     Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
  1794.         " command ?count?\"", (char *) NULL);
  1795.     return TCL_ERROR;
  1796.     }
  1797. #if NO_GETTOD
  1798.     start = times(&dummy2);
  1799. #else
  1800.     gettimeofday(&start, &tz);
  1801. #endif
  1802.     for (i = count ; i > 0; i--) {
  1803.     result = Tcl_Eval(interp, argv[1]);
  1804.     if (result != TCL_OK) {
  1805.         if (result == TCL_ERROR) {
  1806.         char msg[60];
  1807.         sprintf(msg, "\n    (\"time\" body line %d)",
  1808.             interp->errorLine);
  1809.         Tcl_AddErrorInfo(interp, msg);
  1810.         }
  1811.         return result;
  1812.     }
  1813.     }
  1814. #if NO_GETTOD
  1815.     stop = times(&dummy2);
  1816.     timePer = (((double) (stop - start))*1000000.0)/CLK_TCK;
  1817. #else
  1818.     gettimeofday(&stop, &tz);
  1819.     micros = (stop.tv_sec - start.tv_sec)*1000000
  1820.         + (stop.tv_usec - start.tv_usec);
  1821.     timePer = micros;
  1822. #endif
  1823.     Tcl_ResetResult(interp);
  1824.     sprintf(interp->result, "%.0f microseconds per iteration", timePer/count);
  1825.     return TCL_OK;
  1826. }
  1827.  
  1828. /*
  1829.  *----------------------------------------------------------------------
  1830.  *
  1831.  * CleanupChildren --
  1832.  *
  1833.  *    This is a utility procedure used to wait for child processes
  1834.  *    to exit, record information about abnormal exits, and then
  1835.  *    collect any stderr output generated by them.
  1836.  *
  1837.  * Results:
  1838.  *    The return value is a standard Tcl result.  If anything at
  1839.  *    weird happened with the child processes, TCL_ERROR is returned
  1840.  *    and a message is left in interp->result.
  1841.  *
  1842.  * Side effects:
  1843.  *    If the last character of interp->result is a newline, then it
  1844.  *    is removed unless keepNewline is non-zero.  File errorId gets
  1845.  *    closed, and pidPtr is freed back to the storage allocator.
  1846.  *
  1847.  *----------------------------------------------------------------------
  1848.  */
  1849.  
  1850. static int
  1851. CleanupChildren(interp, numPids, pidPtr, errorId, keepNewline)
  1852.     Tcl_Interp *interp;        /* Used for error messages. */
  1853.     int numPids;        /* Number of entries in pidPtr array. */
  1854.     int *pidPtr;        /* Array of process ids of children. */
  1855.     int errorId;        /* File descriptor index for file containing
  1856.                  * stderr output from pipeline.  -1 means
  1857.                  * there isn't any stderr output. */
  1858.     int keepNewline;        /* Non-zero means don't discard trailing
  1859.                  * newline. */
  1860. {
  1861.     int result = TCL_OK;
  1862.     int i, pid, length, abnormalExit;
  1863.     WAIT_STATUS_TYPE waitStatus;
  1864.  
  1865.     abnormalExit = 0;
  1866.     for (i = 0; i < numPids; i++) {
  1867.     do {
  1868.         pid = waitpid(pidPtr[i], (int *) &waitStatus, 0);
  1869.     } while ((pid == -1) && (errno == EINTR));
  1870.     if (pid == -1) {
  1871.         Tcl_AppendResult(interp, "error waiting for process to exit: ",
  1872.             Tcl_PosixError(interp), (char *) NULL);
  1873.         continue;
  1874.     }
  1875.  
  1876.     /*
  1877.      * Create error messages for unusual process exits.  An
  1878.      * extra newline gets appended to each error message, but
  1879.      * it gets removed below (in the same fashion that an
  1880.      * extra newline in the command's output is removed).
  1881.      */
  1882.  
  1883.     if (!WIFEXITED(waitStatus) || (WEXITSTATUS(waitStatus) != 0)) {
  1884.         char msg1[20], msg2[20];
  1885.  
  1886.         result = TCL_ERROR;
  1887.         sprintf(msg1, "%d", pid);
  1888.         if (WIFEXITED(waitStatus)) {
  1889.         sprintf(msg2, "%d", WEXITSTATUS(waitStatus));
  1890.         Tcl_SetErrorCode(interp, "CHILDSTATUS", msg1, msg2,
  1891.             (char *) NULL);
  1892.         abnormalExit = 1;
  1893.         } else if (WIFSIGNALED(waitStatus)) {
  1894.         char *p;
  1895.     
  1896.         p = Tcl_SignalMsg((int) (WTERMSIG(waitStatus)));
  1897.         Tcl_SetErrorCode(interp, "CHILDKILLED", msg1,
  1898.             Tcl_SignalId((int) (WTERMSIG(waitStatus))), p,
  1899.             (char *) NULL);
  1900.         Tcl_AppendResult(interp, "child killed: ", p, "\n",
  1901.             (char *) NULL);
  1902.         } else if (WIFSTOPPED(waitStatus)) {
  1903.         char *p;
  1904.  
  1905.         p = Tcl_SignalMsg((int) (WSTOPSIG(waitStatus)));
  1906.         Tcl_SetErrorCode(interp, "CHILDSUSP", msg1,
  1907.             Tcl_SignalId((int) (WSTOPSIG(waitStatus))), p, (char *) NULL);
  1908.         Tcl_AppendResult(interp, "child suspended: ", p, "\n",
  1909.             (char *) NULL);
  1910.         } else {
  1911.         Tcl_AppendResult(interp,
  1912.             "child wait status didn't make sense\n",
  1913.             (char *) NULL);
  1914.         }
  1915.     }
  1916.     }
  1917.     ckfree((char *) pidPtr);
  1918.  
  1919.     /*
  1920.      * Read the standard error file.  If there's anything there,
  1921.      * then return an error and add the file's contents to the result
  1922.      * string.
  1923.      */
  1924.  
  1925.     if (errorId >= 0) {
  1926.     while (1) {
  1927. #        define BUFFER_SIZE 1000
  1928.         char buffer[BUFFER_SIZE+1];
  1929.         int count;
  1930.     
  1931.         count = read(errorId, buffer, (size_t) BUFFER_SIZE);
  1932.     
  1933.         if (count == 0) {
  1934.         break;
  1935.         }
  1936.         result = TCL_ERROR;
  1937.         if (count < 0) {
  1938.         Tcl_AppendResult(interp,
  1939.             "error reading stderr output file: ",
  1940.             Tcl_PosixError(interp), (char *) NULL);
  1941.         break;
  1942.         }
  1943.         buffer[count] = 0;
  1944.         Tcl_AppendResult(interp, buffer, (char *) NULL);
  1945.     }
  1946.     close(errorId);
  1947.     }
  1948.  
  1949.     /*
  1950.      * If a child exited abnormally but didn't output any error information
  1951.      * at all, generate an error message here.
  1952.      */
  1953.  
  1954.     if (abnormalExit && (*interp->result == 0)) {
  1955.     Tcl_AppendResult(interp, "child process exited abnormally",
  1956.         (char *) NULL);
  1957.     }
  1958.  
  1959.     /*
  1960.      * If the last character of interp->result is a newline, then remove
  1961.      * the newline character (the newline would just confuse things).
  1962.      */
  1963.  
  1964.     length = strlen(interp->result);
  1965.     if (!keepNewline && (length > 0) && (interp->result[length-1] == '\n')) {
  1966.     interp->result[length-1] = '\0';
  1967.     }
  1968.  
  1969.     return result;
  1970. }
  1971.